package com.shujia.sink

import org.apache.flink.api.common.serialization.{SerializationSchema, SimpleStringSchema}
import org.apache.flink.streaming.api.scala._

object Demo02SocketSink {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    // 通过socket构建无界流
    val socketDS: DataStream[String] = env.socketTextStream("master", 8888)

    // word count
    val wordCntDS: DataStream[String] = socketDS
      .flatMap(_.split(","))
      .map(word => (word, 1))
      .keyBy(_._1)
      .sum(1)
      .map(kv => kv._1 + "," + kv._2 + "\n")

    wordCntDS.writeToSocket("master", 9999, new SimpleStringSchema())

    env.execute()

  }

}
